/*
 * Ubiquity command fx update scanner
 */
CmdUtils.CreateCommand({
  names: ["fx update scanner", "update check", "firefox update check", "check for update"],
  icon: "http://www.mozilla.org/images/mozilla-16.png",
  homepage: "http://boardraider-ubiquity-commands.googlecode.com/svn/trunk/fx-update-scanner.html",
  author: { name: "boardraider" },
  license: "GPL 2 or newer",
  description: "Checks for a Firefox application update on startup",
  help: "Executes on startup or on demand",
  
  preview: function( pblock ) {
    pblock.innerHTML = _("Checks for a Firefox application update.");
  },
   
  execute: function() {
    updateScanner(true);
  }
  
});

/*
 * Once called at browser startup
 */
function startup_doScan() {
  Utils.setTimeout(updateScanner, "15000"); // Timeout to ensure browser availability
}

/*
 * Scans for an application update
 *
 * Params:
 * manual - to distinguish between startup or manual call
 */
function updateScanner(manual) {
  
  // Update listener object
  var updateListener = {
    
    onProgress: function(request, position, totalSize) {},
    onError: function(request, update) {},
    
    // When check is complete
    onCheckComplete: function(request, updates, updateCount) {
      
      // Get current notification box
      var browserNoteBox; 
      if (manual) { // When called manually
        browserNoteBox = context.chromeWindow.getBrowser().getNotificationBox();  
      } else { // When called on startup - context undefined
        var chromeWindow = Components.classes["@mozilla.org/appshell/window-mediator;1"]
                                     .getService(Components.interfaces.nsIWindowMediator)
                                     .getMostRecentWindow("navigator:browser");
        browserNoteBox = chromeWindow.getBrowser().getNotificationBox();  
      }
     
      if (updateCount > 0) { // If there is an update
        
        // Button to show the release notes
        var relNotesButton = {
          accessKey: null,
          callback: openRelNotes,
          label: _("Release Notes"),
          popup: null,
          URL: updates[0].detailsURL
        }
            
        var buttons = new Array(relNotesButton);
        browserNoteBox.appendNotification(_("Firefox update check complete - an update is available: ") +
            updates[0].name, _("update"), "http://www.mozilla.org/images/mozilla-16.png", 6, buttons);
        
      } else { // If there is no update
        if (manual) { // Notificate user when check is called manually
          browserNoteBox.appendNotification(_("Firefox update check complete - no update available."),
            _("update"), "http://www.mozilla.org/images/mozilla-16.png", 6, null);
        }
      }
    },
 
    // Support QueryInterface
    QueryInterface: function(aIID) {
      if (!aIID.equals(Components.interfaces.nsIUpdateCheckListener)
          &&
          !aIID.equals(Components.interfaces.nsISupports))
        throw Components.results.NS_ERROR_NO_INTERFACE;
      return this;
    }
  }
      
  // Get update checker 
  var checker = Components.classes["@mozilla.org/updates/update-checker;1"]
                          .createInstance(Components.interfaces
                                                    .nsIUpdateChecker);
  // Force check for an update
  checker.checkForUpdates(updateListener, true);
}

/*
 * Callback for the release notes button
 *
 * Params:
 * note - the <notification> the button is associated with
 * button - the button description as passed to appendNotification
 */
function openRelNotes(note, button) {
  Utils.openUrlInBrowser(button.URL);
}